package com.kun.portal.webframe.po;

import com.fasterxml.jackson.annotation.JsonFormat;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonProperty;
import jdk.nashorn.internal.objects.annotations.Getter;
import org.hibernate.annotations.GenericGenerator;
import org.springframework.format.annotation.DateTimeFormat;

import javax.persistence.*;
import java.io.Serializable;
import java.util.Date;
import java.util.List;
import java.util.Set;

/**
 * Created by Administrator on 2018/5/22.
 */
@Entity
@Table(
        name = "fbo_ground_service_scheduling"
)
public class GroundServiceScheduling implements Serializable {



    public GroundServiceItemPo getGroundServiceItemPo() {
        return groundServiceItemPo;
    }

    public void setGroundServiceItemPo(GroundServiceItemPo groundServiceItemPo) {


        this.groundServiceItemPo = groundServiceItemPo;
    }


    @Id
    @GeneratedValue(
            generator = "gss_id"
    )

    @GenericGenerator(
            name = "gss_id",
            strategy = "uuid"
    )
    @Column(
            name = "gss_id",
            length = 32,
            nullable = false
    )
    private String gssId;

    @Column(
            name = "consume_time",
            length = 32
    )
    @DateTimeFormat(pattern="yyyy-MM-dd HH:mm")
   private Date consumeTime;

    @Column(
            name = "start_date",
            length = 32
    )
    @DateTimeFormat(pattern="yyyy-MM-dd HH:mm:ss")
    private Date startDate;

    @Column(
            name = "end_date",
            length = 32
    )
    @DateTimeFormat(pattern="yyyy-MM-dd HH:mm:ss")
    private Date endDate;

   @ManyToOne
   @JoinColumn(name="id")

   @JsonProperty(access = JsonProperty.Access.WRITE_ONLY)
    private GroundServiceSchedulings groundServiceSchedulings;

//    @ManyToMany(cascade = CascadeType.REFRESH, fetch = FetchType.EAGER)
//    @JoinTable(name = "fbo_service_selected_users",
//            joinColumns = {@JoinColumn(name = "gss_id", referencedColumnName = "gss_id")},
//            inverseJoinColumns = {@JoinColumn(name = "selectable_users_id", referencedColumnName ="selectable_users_id")}
//    )
//    private Set<ServiceItemToSelectableUsersPo> selectedUser;


    @ManyToMany(cascade = CascadeType.DETACH, fetch = FetchType.EAGER)
    @JoinTable(name = "fbo_service_selected_fwusers",
            joinColumns = {@JoinColumn(name = "gss_id", referencedColumnName = "gss_id")},
            inverseJoinColumns = {@JoinColumn(name = "user_id", referencedColumnName ="user_id")}
    )

    private List<FwUserPo> selectedFwUser;

    @ManyToOne(cascade = CascadeType.DETACH, fetch = FetchType.EAGER)
    @JoinColumn(name = "air_security_vehicle_id")

    private AirSecurityVehiclePo airSecurityVehiclePo;
    @Column(
            name = "selected_team_id",
            length = 32

    )
    private String selectedTeamId;
    @ManyToOne(targetEntity =GroundServiceItemPo.class )
    @JoinColumn(name = "gsi_id",referencedColumnName = "gsi_id")
    private GroundServiceItemPo groundServiceItemPo;

    @Column(
            name = "status",
            length = 32

    )
    private String status;

    public String getStatus() {
        return status;
    }

    public void setStatus(String status) {
        this.status = status;
    }

    public String getGssId() {
        return gssId;
    }

    public void setGssId(String gssId) {
        this.gssId = gssId;
    }


    public GroundServiceSchedulings getGroundServiceSchedulings() {
        return groundServiceSchedulings;
    }

    public void setGroundServiceSchedulings(GroundServiceSchedulings groundServiceSchedulings) {
        this.groundServiceSchedulings = groundServiceSchedulings;
    }
    @JsonFormat(pattern="yyyy-MM-dd HH:mm",timezone="Asia/Shanghai")
    public Date getConsumeTime() {
        return consumeTime;
    }

    public void setConsumeTime(Date consumeTime) {
        this.consumeTime = consumeTime;
    }
//
//    public Set<ServiceItemToSelectableUsersPo> getSelectedUser() {
//        return selectedUser;
//    }
//
//    public void setSelectedUser(Set<ServiceItemToSelectableUsersPo> selectedUser) {
//        this.selectedUser = selectedUser;
//    }

    public Date getStartDate() {
        return startDate;
    }

    public void setStartDate(Date startDate) {
        this.startDate = startDate;
    }

    public Date getEndDate() {
        return endDate;
    }

    public void setEndDate(Date endDate) {
        this.endDate = endDate;
    }

    public List<FwUserPo> getSelectedFwUser() {
        return selectedFwUser;
    }

    public void setSelectedFwUser(List<FwUserPo> selectedFwUser) {
        this.selectedFwUser = selectedFwUser;
    }

    public String getSelectedTeamId() {
        return selectedTeamId;
    }

    public void setSelectedTeamId(String selectedTeamId) {
        this.selectedTeamId = selectedTeamId;
    }

    public AirSecurityVehiclePo getAirSecurityVehiclePo() {
        return airSecurityVehiclePo;
    }

    public void setAirSecurityVehiclePo(AirSecurityVehiclePo airSecurityVehiclePo) {
        this.airSecurityVehiclePo = airSecurityVehiclePo;
    }

    public FlightPlanePo getFlightPlanePo(){
        return  this.groundServiceSchedulings.getFlightPlanePo();
    }
    @JsonFormat(pattern="yyyy-MM-dd HH:mm:ss",timezone="Asia/Shanghai")
    public Date getServiceTime(){

                FlightPlanePo flightPlanePo =this.groundServiceSchedulings.getFlightPlanePo();
                String importOrExport = flightPlanePo.getImportOrExport();

                if(importOrExport.equals("进港")){
                   return    new Date(flightPlanePo.getArriveTime().getTime()+this.groundServiceItemPo.getDifference()*60*1000);
                }

                if(importOrExport.equals("出港")){
                    return    new Date(flightPlanePo.getTakeOffTime().getTime()-this.groundServiceItemPo.getDifference()*60*1000);
                }

                if(importOrExport.equals("中转联程")&&this.groundServiceItemPo.getTransferProcessStrategy().equals("takeOffTime")){
                    return    new Date(flightPlanePo.getTakeOffTime().getTime()-this.groundServiceItemPo.getDifference()*60*1000);
                }

                if(importOrExport.equals("中转联程")&&this.groundServiceItemPo.getTransferProcessStrategy().equals("arriveTime")){
                    return    new Date(flightPlanePo.getArriveTime().getTime()+this.groundServiceItemPo.getDifference()*60*1000);
                }
                return null;
    };
}
